﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.Models;
using System.Text;

namespace WebApp.Controllers
{
    public class WidgetsController : ApplicationController
    {
        //
        // GET: /Widget/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Calendar()
        {
            return View();
        }

        public ActionResult Stocks()
        {
            UserData userData = UserData.GetInstance();
            Person user = (Person)ViewData["ProfileModel"];
            if (Request.Form["submit"] != null &&
                Request.Form["action"] != null)
            {
                if (Request.Form["action"].ToString().Equals("add"))
                {
                    try
                    {
                        String symbol = Request.Form["symbol"].ToString().ToUpper();
                        double buyingPrice = Double.Parse(
                            Request.Form["buyPrice"].ToString());
                        long volume = long.Parse(Request.Form["volume"]);
                        Stock stock = new Stock(symbol);
                        stock.BuyingPrice = buyingPrice;
                        stock.Shares = volume;
                        userData.InsertStock(user, stock);
                    }
                    catch(Exception e)
                    {

                    }
                }
                else if (Request.Form["action"].ToString().Equals("save"))
                {
                    
                }
            }
            StockCollection stockCollection = userData.GetStocks(user);
            stockCollection.FetchPrices();
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<String, Stock> pair in stockCollection.Stocks)
            {
                Stock stock = pair.Value;
                sb.Append(stock.Symbol);
                sb.Append(",");
            }
            ViewData["Symbols"] = sb.ToString();
            ViewData["StocksModel"] = stockCollection;
            return View();
        }
    }
}
